#include "DisplayWidget.h"
#include <stdio.h>

DisplayWidget::DisplayWidget(DisplayController* pController, QWidget* parentWidget)
	: QWidget(parentWidget)
{
	QFont font;
	font.setPointSize(14);
    
	setFont(font);
	_pController = pController;
}

void DisplayWidget::paintEvent(QPaintEvent* event)
{
	Redraw();	
}

void DisplayWidget::addDisplay(LMONDisplay* pDisplay)
{
	_display.push_back(pDisplay);
}

void DisplayWidget::Redraw()
{
	for (unsigned int i = 0; i < _display.size(); i++)
	{
		_display[i]->drawSignal(this);
	}
}

void DisplayWidget::calculatePositions()
{
	int y = 0, ymax = 0, height;

	for (unsigned int i = 0; i < _display.size(); i++)
	{
		y = 0;
		if (i>0) {
			if (_display[i]->positionIsShared())
				y = _display[i-1]->getRect().top();
			else
				y = ymax;
		}
		
		height = _display[i]->getHeight();
		if (height == 0) {
			height = QWidget::height() - y;
		}
		ymax = max(y + height, ymax);
		_display[i]->setRect(0, y, width(), height);
	}
}

void DisplayWidget::setTimeScale(int timeScale)
{
	if(timeScale>0)
		for (unsigned int i = 0; i < _display.size(); i++)
			_display[i]->setScaleX(timeScale);
}

QSize DisplayWidget::sizeHint() const
{
    return QSize(300,200);
}

